package com.ysd.servlet;
import java.io.IOException;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.newsDao;
import com.ysd.dao.impl.newsrr;
import com.ysd.entity.news;
import com.ysd.entity.newsuse;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


@WebServlet("/newsrrr")
public class newsrrr extends BaseServlet {
	newsDao dao=new newsDao();
	
	
     public void selectAllJson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			 int page=Integer.parseInt(request.getParameter("page").toString());
			 int size=Integer.parseInt(request.getParameter("limit").toString());
			 String userName=null;
			 if(request.getParameter("userName")!=null && request.getParameter("userName")!="") {
				userName=request.getParameter("userName").toString(); 
			 }
				ArrayList<news> ls=dao.getAllNewsTwo();
				ArrayList<news> lsdata=dao.getAllNewsTwo(page, size, userName);
				Result res=new Result(0, "", ls.size(), lsdata);
			Gson g=new Gson();
			PrintWriter out=response.getWriter();
			out.print(g.toJson(res));
			out.flush();
			out.close();
		}  
			public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
			String years=request.getParameter("years").toString();
			String days=request.getParameter("days").toString();
			String homeFlag=request.getParameter("homeFlag").toString();
			String title=request.getParameter("title").toString();
			String content=request.getParameter("content").toString();
			int i=dao.add(years, days, homeFlag, title, content);
			Result res=new Result(200, "true", i, "success");
			Gson g=new Gson();
			PrintWriter out=response.getWriter();
			out.print(g.toJson(res));
			out.flush();
			out.close();
			}  	
			public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				String years=request.getParameter("years").toString();
				String days=request.getParameter("days").toString();
				String homeFlag=request.getParameter("homeFlag").toString();
				String title=request.getParameter("title").toString();
				String content=request.getParameter("content").toString();
			int newId=Integer.parseInt(request.getParameter("newId").toString());
			System.out.println(title);
			int i=dao.update(newId, years, days, homeFlag, title, content);
			Result res=new Result(200, "true", i, "success");
			Gson g=new Gson();
			PrintWriter out=response.getWriter();
			out.print(g.toJson(res));
			out.flush();
			out.close();
			}
			public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				PrintWriter out=response.getWriter();
				int newId=Integer.parseInt(request.getParameter("newId").toString());
			int i=dao.delete(newId);
			Result res=new Result(200, "", i, "success");
			Gson g=new Gson();
			out.print(g.toJson(res));
			out.flush();
			out.close();
			}

}
